package com.boyu.front;

import com.boyu.common.BaseController;
import com.boyu.common.SystemConst;
import com.boyu.common.model.Product;
import com.boyu.common.model.ProductAttr;
import com.boyu.common.model.ProductImage;
import com.jfinal.plugin.activerecord.Page;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * {@link ProductController}
 */
public class ProductController extends BaseController {
	static String product_o_set_str = "product_o_set";
	static String product_a_set_str = "product_a_set";
	public void index() {
		String redirectStr= "/product?1=1";
		Integer pageNum = getParaToInt("pageNum",1);
		Enumeration<String> paraNames = getParaNames();
		String from = "from t_product where 1=1 ";
		while (paraNames.hasMoreElements()) {
			String paraName = paraNames.nextElement();
			if (paraName.startsWith("a_")) {
				String paraNameTemp = paraName.replaceAll("a_","");
				String para = getPara(paraName);
				from += " and " + paraNameTemp + "=" + para;
				setAttr(paraName,para);
				redirectStr += "&" + paraName + "=" + para;
			}
		}
		from = from + " order by id asc";
		Page<Product> productPage = Product.dao.paginate(pageNum, PAGE_SIZE, "select *", from);
		List<ProductAttr> productAttrList = ProductAttr.dao.find("select * from t_product_attr");
		setAttr("productAttrList", productAttrList);
		setAttr("productPage", productPage);
		setAttr("redirectStr",redirectStr);
		render(LIST_HTML);
	}

	public void detail() {
		String productId = getPara("id");
		Product product = Product.dao.findById(productId);
		List<ProductImage> productImageList = ProductImage.dao.find("select * from t_product_image where productId = ? and isDel = 0", productId);
		List<ProductAttr> productAttrList = ProductAttr.dao.find("select * from t_product_attr");
		setAttr("productAttrList", productAttrList);
		setAttr("product", product);
		setAttr("productImageList",productImageList);
		setAttr(SystemConst.FRONT_TITLE,product.getName());
		removeAttr(DETAIL_HTML);
	}
}





